<?php

namespace App\Http\Controllers\Api;

use App\Models\ImGroup;
use App\Models\User;
use App\Models\Verification;
use Illuminate\Http\Request;
use App\Models\ImUserFriend;
use App\Models\ImCollect;
use App\Models\ImCollectFile;
use App\Models\ImDevice;
use App\Models\ImReportCate;
use App\Models\ImReport;
use App\Models\ImChatTop;
use App\Models\ImGroupUser;
use App\Models\ImUserNoticeStatus;
use App\Models\ImUserWay;
use App\Models\ImUserCurrent;
use App\Models\ImFeedback;
use App\Models\ImCondition;
use App\Models\ImUserCondition;
use App\Models\ImNotification;
use App\Models\ImUserForward;
use Illuminate\Support\Facades\Validator;


class UserController
{


    /**
     * @desc 添加好友申请
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/25 16:25
     */
    public function addFriend(Request $request, ImUserFriend $ImUserFriendModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImUserFriendModel->addFriend($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc  审核好友
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/25 16:25
     */
    public function auditFriend(Request $request, ImUserFriend $ImUserFriendModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImUserFriendModel->auditFriend($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc  删除好友
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/25 16:25
     */
    public function delFriend(Request $request, ImUserFriend $ImUserFriendModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImUserFriendModel->delFriend($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 好友列表
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/25 17:23
     */
    public function friendList(Request $request, ImUserFriend $ImUserFriendModel)
    {
        $limit = (int) $request->query('limit', 10);
        $offset = (int) $request->query('offset', 0);
        $user = $request->user();
        $userId = $user->id;
        $list = $ImUserFriendModel->friendList($userId, $limit, $offset);

        return okBack($list);
    }

    /**
     * @desc 黑名单管理列表
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/25 17:23
     */
    public function blackFriendList(Request $request, ImUserFriend $ImUserFriendModel)
    {
        $limit = (int) $request->query('limit', 10);
        $offset = (int) $request->query('offset', 0);
        $user = $request->user();
        $userId = $user->id;
        $list = $ImUserFriendModel->blackFriendList($userId, $limit, $offset);

        return okBack($list);
    }

    /**
     * @desc 添加用户到好友黑名单
     * @param Request $request
     * @param ImGroupBlack $ImGroupBlackModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/24 11:05
     */
    public function addUserBlack(Request $request, ImUserFriend $ImUserFriendModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImUserFriendModel->addUserBlack($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 好友移除黑名单
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/25 18:04
     */
    public function delUserBlack(Request $request, ImUserFriend $ImUserFriendModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImUserFriendModel->delUserBlack($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 添加进我的收藏
     * @param Request $request
     * @param ImCollect $ImCollectModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/29 12:41
     */
    public function addCollent(Request $request, ImCollect $ImCollectModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImCollectModel->addCollent($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 我的收藏列表
     * @param Request $request
     * @param ImCollect $ImCollectModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/29 15:47
     */
    public function collectList(Request $request, ImCollect $ImCollectModel)
    {
        $limit = (int) $request->query('limit', 10);
        $offset = (int) $request->query('offset', 0);
        $user = $request->user();
        $userId = $user->id;
        $list = $ImCollectModel->collectList($userId, $limit, $offset);

        return okBack($list);
    }

    /**
     * @desc 删除设备
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/25 18:04
     */
    public function delDevice(Request $request, ImDevice $ImDeviceModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImDeviceModel->delDevice($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 注销用户
     * @param Request $request
     * @param User $UserModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/30 18:32
     */
    public function delUserAccount(Request $request, User $UserModel)
    {
        $input = $request->all();
        $messages = [
            'phone.required' => '手机号不能为空',
            //'phone.unique'    => '手机号已被注册',
            'phone.max' => '手机号最大为11位字符',
            'phone.min' => '手机号最小为11位字符',
            'captcha.required' => '验证码不能为空',
            'captcha.min' => '验证码最少为4位字符',
        ];
        $validator = Validator::make($input, [
            'phone' => 'required|string|max:11|min:11',
            'captcha' => 'required|min:4',
        ], $messages);

        if($validator->fails())
        {
            return errBack($validator->errors()->first(), 400);
        }

        $areaCode = $input['area_code'] ?? 86;
        $verify = Verification::where('account', $input['phone'])
            ->where('channel', 'sms')
            ->where('area_code', $areaCode)
            ->where('code', $input['captcha'])
            ->orderby('id', 'desc')
            ->first();
        if(!$verify)
        {
            return errBack('验证码错误或者已失效', 401);
        }

        $user = $request->user();
        $userId = $user->id;
        $res = $UserModel->delUserAccount($userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 新好友列表
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/25 17:23
     */
    public function newFriendList(Request $request, ImUserFriend $ImUserFriendModel)
    {
        $limit = (int) $request->query('limit', 20);
        $offset = (int) $request->query('offset', 0);
        $user = $request->user();
        $userId = $user->id;
        $list = $ImUserFriendModel->newFriendList($userId, $limit, $offset);

        return okBack($list);
    }

    /**
     * @desc 手机联系人
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/25 17:23
     */
    public function phoneFriendList(Request $request, ImUserFriend $ImUserFriendModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $list = $ImUserFriendModel->phoneFriendList($input, $userId);

        return okBack($list);
    }

    /**
     * @desc 修改好友资料
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/25 16:25
     */
    public function setFriendAllow(Request $request, ImUserFriend $ImUserFriendModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImUserFriendModel->setFriendAllow($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 修改好友备注
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/25 16:25
     */
    public function setFriendNote(Request $request, ImUserFriend $ImUserFriendModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImUserFriendModel->setFriendNote($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 修改好友聊天背景
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/25 16:25
     */
    public function setFriendCover(Request $request, ImUserFriend $ImUserFriendModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImUserFriendModel->setFriendCover($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 查看好友资料
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/25 16:25
     */
    public function goodFriend(Request $request, ImUserFriend $ImUserFriendModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImUserFriendModel->goodFriend($input, $userId);

        return okBack($res);
    }

    /**
     * @desc 举报类型列表
     * @param Request $request
     * @param ImRole $ImRoleModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/23 10:53
     */
    public function reportCateList(ImReportCate $ImReportCateModel)
    {
        $list = $ImReportCateModel->get();

        return okBack($list);
    }

    /**
     * @desc 举报聊天证明
     * @param Request $request
     * @param ImCollect $ImCollectModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/29 12:41
     */
    public function addReportChat(Request $request, ImReport $ImReportModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImReportModel->addReportChat($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 群聊私聊置顶
     * @param Request $request
     * @param ImChatTop $ImChatTopModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/12/1 17:31
     */
    public function addTopChat(Request $request, ImChatTop $ImChatTopModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImChatTopModel->addTopChat($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 设置单聊免打扰
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/25 16:25
     */
    public function setFriendDisturb(Request $request, ImUserFriend $ImUserFriendModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImUserFriendModel->setFriendDisturb($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 群聊私聊置顶
     * @param Request $request
     * @param ImChatTop $ImChatTopModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/12/1 17:31
     */
    public function addTopGroupChat(Request $request, ImChatTop $ImChatTopModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImChatTopModel->addTopGroupChat($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 设置单聊免打扰
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/25 16:25
     */
    public function setGruopDisturb(Request $request, ImGroupUser $ImGroupUserModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImGroupUserModel->setGruopDisturb($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 设置群聊显示群成员昵称
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/25 16:25
     */
    public function setGroupShowName(Request $request, ImGroupUser $ImGroupUserModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImGroupUserModel->setGroupShowName($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 修改好友聊天背景
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/25 16:25
     */
    public function setGroupCover(Request $request, ImGroupUser $ImGroupUserModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImGroupUserModel->setGroupCover($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 修改群聊中自己的备注
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/25 16:25
     */
    public function setGroupNote(Request $request, ImGroupUser $ImGroupUserModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImGroupUserModel->setGroupNote($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 设置群公告
     * @param Request $request
     * @param ImGroup $ImGroupModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/23 15:39
     */
    public function setGroupSummery(Request $request, ImGroup $ImGroupModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImGroupModel->setGroupSummery($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * 点击搜索用户
     *
     * @param \Illuminate\Http\Request $request
     * @param Request $request
     * @return mixed
     * @author zhouzhou 2021/12/3
     */
    public function search(Request $request, User $UserModel)
    {
        $page = (int) $request->query('page', 1); //第几页
        $limit = (int) $request->query('limit', 10); //每页15个
        $search = $request->input('name');
        $offset = ($page - 1) * $limit;
        $user = $request->user();
        $userId = $user->id;

        $list =  $UserModel->searchList($limit, $offset, $search, $userId);

        return okBack($list);
    }

    /**
     * @desc 用户个人资料修改
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/12/3 16:25
     */
    public function editInfo(Request $request, User $UserModel)
    {
        $input = $request->all();
        $user = $request->user();
        $res = $UserModel->editInfo($input, $user);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 修改消息设置
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/12/3 16:25
     */
    public function setNotice(Request $request, ImUserNoticeStatus $ImUserNoticeStatusModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImUserNoticeStatusModel->setNotice($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 设置我的隐私添加方式
     * @param Request $request
     * @param ImUserWay $ImUserWayModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/12/3 16:22
     */
    public function setWay(Request $request, ImUserWay $ImUserWayModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImUserWayModel->setWay($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 设置我的通用
     * @param Request $request
     * @param ImUserCurrent $ImUserCurrentModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/12/3 16:22
     */
    public function setCurrent(Request $request, ImUserCurrent $ImUserCurrentModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImUserCurrentModel->setCurrent($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 意见反馈表
     * @param Request $request
     * @param ImCollect $ImCollectModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/29 12:41
     */
    public function addFeedback(Request $request, ImFeedback $ImFeedbackModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImFeedbackModel->addFeedback($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 隐私不让对方看
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/25 17:23
     */
    public function noAllowLookList(Request $request, ImUserFriend $ImUserFriendModel)
    {
        $limit = (int) $request->query('limit', 10);
        $offset = (int) $request->query('offset', 0);
        $user = $request->user();
        $userId = $user->id;
        $list = $ImUserFriendModel->noAllowLookList($userId, $limit, $offset);

        return okBack($list);
    }

    /**
     * @desc 隐私不看对方
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/25 17:23
     */
    public function noLookOtherList(Request $request, ImUserFriend $ImUserFriendModel)
    {
        $limit = (int) $request->query('limit', 10);
        $offset = (int) $request->query('offset', 0);
        $user = $request->user();
        $userId = $user->id;
        $list = $ImUserFriendModel->noLookOtherList($userId, $limit, $offset);

        return okBack($list);
    }

    /**
     * @desc 不让对方看我的朋友圈移除，支持多个
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/12/6 18:04
     */
    public function delAllowLook(Request $request, ImUserFriend $ImUserFriendModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImUserFriendModel->delAllowLook($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 添加多个不让对方看我的朋友圈
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/12/6 18:04
     */
    public function addAllowLook(Request $request, ImUserFriend $ImUserFriendModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImUserFriendModel->addAllowLook($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 不看对方的朋友圈移除，支持多个，
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/12/6 18:04
     */
    public function delLookOther(Request $request, ImUserFriend $ImUserFriendModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImUserFriendModel->delLookOther($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 添加不看对方的朋友圈添加，支持多个
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/12/6 18:04
     */
    public function addLookOther(Request $request, ImUserFriend $ImUserFriendModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImUserFriendModel->addLookOther($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 好友列表
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/25 17:23
     */
    public function groupFriendList(Request $request, ImUserFriend $ImUserFriendModel)
    {
        $limit = (int) $request->query('limit', 10);
        $offset = (int) $request->query('offset', 0);
        $imGid = $request->query('im_gid', '');
        $user = $request->user();
        $userId = $user->id;
        $list = $ImUserFriendModel->groupFriendList($imGid, $userId, $limit, $offset);

        return okBack($list);
    }

    /**
     * @desc 好友列表
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/25 17:23
     */
    public function conditionList(Request $request, ImCondition $ImConditionModel)
    {
        $user = $request->user();
        $userId = $user->id;
        $list = $ImConditionModel->conditionList($userId);

        return okBack($list);
    }

    /**
     * @desc 设置用户的状态，24小时有效
     * @param Request $request
     * @param ImUserCondition $ImUserConditionModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/12/8 14:51
     */
    public function setCondition(Request $request, ImUserCondition $ImUserConditionModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImUserConditionModel->setCondition($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 删除我的收藏
     * @param Request $request
     * @param ImCollect $ImCollectModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/29 12:41
     */
    public function delCollent(Request $request, ImCollect $ImCollectModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImCollectModel->delCollent($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * 点击搜索用户收藏
     *
     * @param \Illuminate\Http\Request $request
     * @param Request $request
     * @return mixed
     * @author zhouzhou 2021/12/13
     */
    public function searchCollect(Request $request, ImCollect $ImCollectModel)
    {
        $page = (int) $request->query('page', 1); //第几页
        $limit = (int) $request->query('limit', 10); //每页15个
        $search = $request->input('search');
        $type = $request->input('type');
        $recent = $request->input('recent', 0);
        $offset = ($page - 1) * $limit;
        $user = $request->user();
        $userId = $user->id;

        $list =  $ImCollectModel->searchCollect($limit, $offset, $search, $userId, $type, $recent);

        return okBack($list);
    }

    /**
     * @desc 不允许对方看的好友列表
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/25 17:23
     */
    public function noAllowFriendList(Request $request, ImUserFriend $ImUserFriendModel)
    {
        $limit = (int) $request->query('limit', 10);
        $offset = (int) $request->query('offset', 0);
        $user = $request->user();
        $userId = $user->id;
        $list = $ImUserFriendModel->noAllowFriendList($userId, $limit, $offset);

        return okBack($list);
    }

    /**
     * @desc 不看对方看的好友列表
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/25 17:23
     */
    public function noLookFriendList(Request $request, ImUserFriend $ImUserFriendModel)
    {
        $limit = (int) $request->query('limit', 10);
        $offset = (int) $request->query('offset', 0);
        $user = $request->user();
        $userId = $user->id;
        $list = $ImUserFriendModel->noLookFriendList($userId, $limit, $offset);

        return okBack($list);
    }

    /**
     * @desc 消息通知
     * @param Request $request
     * @param ImNotification $ImNotificationModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/12/16 18:04
     */
    public function noticeList(Request $request, ImNotification $ImNotificationModel)
    {
        $limit = (int) $request->query('limit', 10);
        $offset = (int) $request->query('offset', 0);
        $user = $request->user();
        $userId = $user->id;
        $list = $ImNotificationModel->noticeList($userId, $limit, $offset);

        return okBack($list);
    }

    /**
     * @desc 删除
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/25 18:04
     */
    public function delNotice(Request $request, ImNotification $ImNotificationModel)
    {
        $user = $request->user();
        $userId = $user->id;
        $res = $ImNotificationModel->delNotice($userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 共同的群聊
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/25 17:23
     */
    public function commonGroup(Request $request, ImGroupUser $ImGroupUserModel)
    {
        $limit = (int) $request->query('limit', 10);
        $offset = (int) $request->query('offset', 0);
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $list = $ImGroupUserModel->commonGroup($input, $userId, $limit, $offset);

        return okBack($list);
    }

    /**
     * @desc 添加转发
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/12/6 18:04
     */
    public function addForward(Request $request, ImUserForward $ImUserForwardModel)
    {
        $input = $request->all();
        $user = $request->user();
        $userId = $user->id;
        $res = $ImUserForwardModel->addForward($input, $userId);
        if ($res['code'] == 0)
        {
            return okBack($res['msg']);
        }
        else
        {
            return errBack($res['msg']);
        }
    }

    /**
     * @desc 用户转发的记录表
     * @param Request $request
     * @param ImUserFriend $ImUserFriendModel
     * @return \Illuminate\Http\JsonResponse
     * @author zhouzhou
     * @time 2021/11/25 17:23
     */
    public function forwardList(Request $request, ImUserForward $ImUserForwardModel)
    {
        $limit = (int) $request->query('limit', 10);
        $offset = (int) $request->query('offset', 0);
        $user = $request->user();
        $userId = $user->id;
        $list = $ImUserForwardModel->forwardList($userId, $limit, $offset);

        return okBack($list);
    }

    /**
     * 监听用户上线下线
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @author an
     * @time 2021-12-27 10:39
     */
    public function userStatus(Request $request)
    {
        $userId = $request->user()->id;
        $online = $request->post('online');
        if (!is_numeric($online)) {
            return errBack('状态有误');
        }
        $user = User::find($userId);
        $user->online = $online;
        $user->save();
        return okBack();
    }


}
